277C - Game - CodeForces Solution


games implementation *2400

Please click on ads to support us..

C++ Code:

#include<iostream>
#include<algorithm>

using namespace std;

struct CutSegment {
	int k, l, r;
} cuts[220000];

int ans;

CutSegment make(int k, int l, int r) {
	if (l > r)
		swap(l, r);

	CutSegment res = {k, l, r};

	return res;
}

bool cmp(const CutSegment &a, const CutSegment &b) {
	return a.k < b.k || a.k == b.k && a.l < b.l;
}

void answer(int dir, int k, int l, int r) {
	if (dir == 0)
		cout << k << " " << l << " " << k << " " << r << endl;
	else 
		cout << l << " " << k << " " << r << " " << k << endl;
	exit(0);
}

void work(int dir, int l, int r, int N, int M, int fl) {
	int j, currR, res;
	int num = 0;
	int optimalMove = 0;

	if (N && (l > r || cuts[l].k != 1))
		optimalMove = 1;
	 
    for (int i = l; i <= r; i = j) {
		res = 0;
		currR = 0;
		for (j = i; cuts[j].k == cuts[i].k; ++j) {
			if (cuts[j].l > currR) {
				res += cuts[j].l - currR;
				currR = cuts[j].r;
			} else {
				currR = max(currR, cuts[j].r);
			}
		}
        
        res += M - currR;

        if (fl) {
			if (res >= (res^ans)) {
				int need = res - (res^ans);
				currR = 0;

				for (int k = i; k < j; ++k) {
					if (cuts[k].l > currR) {
						if (cuts[k].l - currR < need) {
							need -= cuts[k].l - currR;
						} else {
							answer(dir, cuts[k].k, 0, currR + need);
						}
						currR = cuts[k].r;
					} else {
						currR = max(currR, cuts[k].r);
					}
				}
				answer(dir, cuts[i].k, 0, currR + need);
			}
		} else {
			ans ^= res;
		}
		num++;

		if (cuts[i].k != N && cuts[i].k + 1 != cuts[j].k)
			optimalMove = cuts[i].k + 1;
    }

    if (!fl) {
		if ((N - num) & 1)
			ans ^= M;
	} else {
		if(optimalMove && M >= (M^ans))
			answer(dir, optimalMove, 0, M - (M^ans));
	}
}

int main() {
    int n, m, k;
	cin >> n >> m >> k;

	int tot1 = 0; 
	int tot2 = k;

	for (int i = 1; i <= k; ++i) {
		int x1, y1, x2, y2;
		cin >> x1 >> y1 >> x2 >> y2;
		if (x1 == x2)
			cuts[++tot1] = make(x1, y1, y2);
		else 
			cuts[++tot2] = make(y1, x1, x2);
	}

    sort(cuts + 1, cuts + tot1 + 1, cmp);
	sort(cuts + k + 1, cuts + tot2 + 1, cmp);
	work(0, 1, tot1, n - 1, m, 0);
	work(1, k + 1, tot2, m - 1, n, 0);

    if(ans) {
		cout << "FIRST" << endl;
		work(0, 1, tot1, n - 1, m, 1);
		work(1, k + 1, tot2, m - 1, n, 1);
	} else {
		cout << "SECOND" << endl;
	}

    return 0;
}/*1693390724.3280349*/


Comments

Submit
0 Comments
More Questions

580A- Kefa and First Steps
1472B- Fair Division
996A - Hit the Lottery
MSNSADM1 Football
MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character
1441. Build an Array With Stack Operations
1356. Sort Integers by The Number of 1 Bits
922. Sort Array By Parity II
344. Reverse String
1047. Remove All Adjacent Duplicates In String
977. Squares of a Sorted Array
852. Peak Index in a Mountain Array
461. Hamming Distance
1748. Sum of Unique Elements
897. Increasing Order Search Tree
905. Sort Array By Parity
1351. Count Negative Numbers in a Sorted Matrix
617. Merge Two Binary Trees
1450. Number of Students Doing Homework at a Given Time
700. Search in a Binary Search Tree
590. N-ary Tree Postorder Traversal
589. N-ary Tree Preorder Traversal
1299. Replace Elements with Greatest Element on Right Side
1768. Merge Strings Alternately
561. Array Partition I
1374. Generate a String With Characters That Have Odd Counts